<?php
class News_category
{
	const DBTable = "tbl_news_category";

	public $Category_id;	public $Title;	public $Status;	public $Is_deleted;	public $Createdate;	public $Updatedate;
	public $Ordering;
	public $Url_slug;
	private $__mysql;

	public function __construct()
	{
		$this->Category_id = 0;		$this->Title = null;		$this->Status = 0;		$this->Is_deleted = 0;		$this->Createdate = date("Y-m-d H:i:s");		$this->Updatedate = date("Y-m-d H:i:s");
		$this->Ordering = 0;
		$this->Url_slug = null;
		$this->__mysql = new MYSQL;
	}

	public function Save()
	{
		$sqlStr = "insert into ".self::DBTable."
				(title, status, createdate, updatedate, url_slug)
				values
				(
				'".mysql_real_escape_string($this->Title)."', 				'".$this->Status."', 				'".$this->Createdate."', 				'".$this->Updatedate."',
				'".clean_url_accents(mysql_real_escape_string($this->Title))."'
				)";
			$this->__mysql->Exec_query($sqlStr);
			$this->Category_id = $this->__mysql->Get_insert_id();
			return $this->Category_id;
	}

	public function Update()
	{
		$sqlStr = "update ".self::DBTable." set
			title = '".mysql_real_escape_string($this->Title)."', 			status = '".$this->Status."', 			updatedate = '".$this->Updatedate."', 
			url_slug = '".clean_url_accents(mysql_real_escape_string($this->Title))."'
			where category_id = '".mysql_real_escape_string($this->Category_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Update_ordering($id_array = null, $category_id = 0)
	{
		if(count($id_array) > 0)
		{
			$i = 1;
			foreach($id_array as $id)
			{
				//update the ordering of the banner that is about to be replaced
				$sqlStr = "update ".self::DBTable." 
					set ordering = ".$i." 
					where 
					category_id = '".$id."'";
				//echo $sqlStr . "<br />";
				$this->__mysql->Exec_query($sqlStr);
				$i++;
			}
		}
	}	

	public function Quick_update($column = null, $value = null)
	{
		$sqlStr = "update ".self::DBTable." set
			".$column." = '".mysql_real_escape_string($value)."',
			 updatedate = '".$this->Updatedate."' 
			where category_id = '".mysql_real_escape_string($this->Category_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function List_all($limit = null, $offset = null)
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where is_deleted = 0 
			order by ordering asc";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Get_categories_assigned_to_news($news_id = 0)
	{
		$sqlStr = "select nc.category_id, nc.title from 
			tbl_news_category_link ncl
			inner join tbl_news_category nc on ncl.category_id = nc.category_id
			where ncl.news_id = ".$news_id."
			order by nc.title asc
			";
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array_assoc($recordset);
	}
	
	public function List_frontend_categories($is_insideamv = 0)
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where is_deleted = 0 and 
			is_insideamv = ".$is_insideamv."
			order by ordering asc";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function List_all_selectlist()
	{
		$sqlStr = "select category_id, title
			from ".self::DBTable."
			where is_deleted = 0 
			order by ordering asc";
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Delete($category_id = 0)
	{
		$sqlStr = "update ".self::DBTable." set is_deleted = 1 where category_id = '".mysql_real_escape_string($category_id)."'";
			$this->__mysql->Exec_query($sqlStr);

		//delete the category from the tbl_news_category_link
		$sqlStr = "delete from tbl_news_category_link where category_id = '".mysql_real_escape_string($category_id)."'";
			$this->__mysql->Exec_query($sqlStr);
			
		$sqlStr = "optimize table ".self::DBTable.", tbl_news_category_link";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Select($category_id = 0)
	{
		$sqlStr = "select * from ".self::DBTable." where category_id = '".mysql_real_escape_string($category_id)."'";
		$recordset = $this->__mysql->Exec_query($sqlStr);

		if($this->__mysql->Get_num_rows($recordset) > 0)
		{
			$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Category_id = $result['category_id'];				$this->Title = $result['title'];				$this->Status = $result['status'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
				$this->Ordering = $result['ordering'];
				$this->Url_slug = $result['url_slug'];
			return true;
		}
		else
		{
			return false;
		}
	}

	public function Select_by($fieldname = null, $value = null)
	{
		if(!is_null($fieldname))
		{
			$sqlStr = "select * from ".self::DBTable." where ".$fieldname." = '".mysql_real_escape_string($value)."'";
			$recordset = $this->__mysql->Exec_query($sqlStr);

			if($this->__mysql->Get_num_rows($recordset) > 0)
			{
				$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Category_id = $result['category_id'];				$this->Title = $result['title'];				$this->Status = $result['status'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
				$this->Ordering = $result['ordering'];
				$this->Url_slug = $result['url_slug'];
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}
?>